package com.tenglong.teacher.ui.answerq;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.tenglong.teacher.R;
import com.tenglong.teacher.ui.port.MainFragmentRubsh;
import com.tenglong.teacher.ui.answerq.answer.MyAnswerFragment;
import com.tenglong.teacher.ui.answerq.relate.RelateAnswerFragment;

/**
 * Created by Host-0 on 2017/3/8.
 */

public class FragmentAnswerQ extends Fragment implements RadioGroup.OnCheckedChangeListener, MainFragmentRubsh {

    private static final String KEY_HEADER_ANSWER_POSITION = "key_header_answer_position";

    private static final String MY_ANSWER = "my_answer";
    private static final String RELATE_ANSWER = "relate_answer";
    private String[] tags = new String[]{MY_ANSWER, RELATE_ANSWER};
    private RadioGroup headerRadioGroup;
    private int mLastTabPosition = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HEADER_ANSWER_POSITION)) {
            mLastTabPosition = savedInstanceState.getInt(KEY_HEADER_ANSWER_POSITION);
        }
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_answerq, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_HEADER_ANSWER_POSITION, mLastTabPosition);
    }

    private void initView(View view) {
        headerRadioGroup = view.findViewById(R.id.header_navigation);
        headerRadioGroup.setOnCheckedChangeListener(this);
        onCheckedChanged(null, mLastTabPosition == 0 ? R.id.myAnswer : R.id.relateAnswer);
    }

    private Fragment getFragment(String tag) {
        FragmentManager manager = getChildFragmentManager();
        Fragment fragment = manager.findFragmentByTag(tag);
        switch (tag) {
            case MY_ANSWER:
                return fragment == null ? new MyAnswerFragment() : fragment;
            default:
                return fragment == null ? new RelateAnswerFragment() : fragment;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.myAnswer:
                setFragmentToRes(tags[0], tags[mLastTabPosition]);
                mLastTabPosition = 0;
                break;
            default:
                setFragmentToRes(tags[1], tags[mLastTabPosition]);
                mLastTabPosition = 1;
                break;
        }
        refresh();
    }


    private void setFragmentToRes(String tag, String hideTag) {
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment showFragment = manager.findFragmentByTag(tag);
        Fragment hideFragment = manager.findFragmentByTag(hideTag);
        if (showFragment == null) {
            transaction.add(R.id.anserques_container, getFragment(tag), tag);
        } else {
            transaction.show(showFragment);
        }
        if (hideFragment != null && !tag.equals(hideTag)) {
            transaction.hide(hideFragment);
        }
        transaction.commit();
    }


    public boolean isFragmentExit(String tag) {
        FragmentManager manager = getChildFragmentManager();
        Fragment f = manager.findFragmentByTag(tag);
        return f != null;
    }

    @Override
    public void refresh() {
        if (isFragmentExit(tags[mLastTabPosition]) && !isHidden()) {
            Fragment f = getFragment(tags[mLastTabPosition]);
            if (f instanceof MainFragmentRubsh) {
                ((MainFragmentRubsh) f).refresh();
            }
        }
    }
}
